package com.yanqu.road.server.cmd.activity.orewar;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityRewardResult;
import com.yanqu.road.entity.enums.activity.eActivityOreWarType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.pb.orewaractivity.OreWarActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.orewar.OreWarActivityModule;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_ORE_WAR_ACTIVITY_GET_REWARD, desc = "领取盐场活动奖励")
public class OreWarActivityGetRewardCmd extends AbstractCommand {

    private int activityId;

    private int conditionId;

    private ActivityRewardResult result;

    private ActivityInfo activityInfo;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        OreWarActivityProto.OreWarActivityGetRewardReqMsg reqMsg = OreWarActivityProto.OreWarActivityGetRewardReqMsg.parseFrom(packet.getBytes());
        conditionId = reqMsg.getConditionId();
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityConditionInfo conditionInfo = OreWarActivityMgr.getActivityConditionInfo(activityId,conditionId);
        if(conditionInfo != null){
            if (OreWarActivityMgr.CONDITION_TYPE_USER != conditionInfo.getType() && OreWarActivityMgr.CONDITION_TYPE_UNION != conditionInfo.getType()) {
                ret = GameErrorCode.E_ORE_ACTIVITY_GET_REWARD_TYPE_ERROR;
            }
            activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
            if(activityInfo != null){
                if(activityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()){
                    result = player.getModule(OreWarActivityModule.class).getRankReward(activityId, conditionInfo.getType());
                }else if(activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
                    result = player.getModule(OreWarActivityModule.class).sendNewCrossRankReward(activityId,conditionId, conditionInfo.getType());
                }
            }
        }else{
            ret = GameErrorCode.E_ORE_ACTIVITY_GET_REWARD_TYPE_ERROR;
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        OreWarActivityProto.OreWarActivityGetRewardRespMsg.Builder respMsg = OreWarActivityProto.OreWarActivityGetRewardRespMsg.newBuilder();
        respMsg.setRet(ret);
        if(0 == ret){
            if(null != result) {
                respMsg.setRet(result.getRet());
                respMsg.setReward(result.getReward());
            }
        }
        if(activityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()){
            player.sendPacket(Protocol.U_ORE_WAR_ACTIVITY_GET_REWARD, respMsg);
        }else{
            if(respMsg.getRet() != 0){
                player.sendPacket(Protocol.U_ORE_WAR_ACTIVITY_GET_REWARD, respMsg);
            }
        }
    }
}
